package pre.liuxl.blog.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import pre.liuxl.blog.entity.User;
import pre.liuxl.blog.repository.UserRepository;
import pre.liuxl.blog.service.UserService;

import javax.servlet.http.HttpSession;

/**
 * Created by liuxuanlong on 2018/6/16.
 */
@Controller
public class LoginController {
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    /**
     * loginCheck
     *
     * @param userName
     * @param password
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/loginCheck")
    public String loginCheck(String userName, String password, HttpSession session, Model model) {
        User user = userService.loginCheck(userName, password);
        session.setAttribute("loginUser", user);

        if (null == user) {

            model.addAttribute("msg", "用户名或密码错误");
            return "login";
        }

        return "redirect:index";
    }
}
